package com.kylin.common.security.service.handler;

import com.kylin.common.security.domain.LoginUser;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户名密码登录(示例，需要在认证服务中单独实现)
 * @author wuhao
 * @version 1.0 - 2022/10/10
 */
@Slf4j
//@Component("demoPasswordLoginHandle")
public class DemoPasswordLoginHandler implements LoginHandler {
    // 认证模式
    private static final String GRANT_TYPE = "demo";
    // 参数名称
    private static final String PARAM_USERNAME = "username";
    private static final String PARAM_PASSWORD = "password";

    @Override
    public LoginUser handle(HttpServletRequest request) {
        String username = request.getParameter(PARAM_USERNAME);
        String password = request.getParameter(PARAM_PASSWORD);
        // 认证处理..... 成功返回用户,其他抛出异常
        log.info(username,password);
        Set<String> authorities = new HashSet<>();
        authorities.add("test");
        authorities.add("system:config:*");
        authorities.add("system:config");
        authorities.add("*:*:*");

        // 获取权限
        LoginUser user = new LoginUser(1L,username, password);
        user.setPermissions(authorities);
        return user;
    }
    
    /**
     * 是否支持该授权模式处理
     * @param grantType 授权模式
     * @return
     * @author wuhao
     * @since 2.6.0 - 2022/10/10
     */
    @Override
    public boolean supports(String grantType) {
        return GRANT_TYPE.equalsIgnoreCase(grantType);
    }

    @Override
    public String getGrantType() {
        return GRANT_TYPE;
    }
}
